﻿#ifndef TIANYU_PROXY_SERVICE_H_
#define TIANYU_PROXY_SERVICE_H_

#ifdef _MSC_VER
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif
#endif

#include "../share/common_singleton.h"
#include "../share/server_base.h"
#include "../share/proxy_channel.h"
#include "../share/channel_base.h"
#include "../share/server_net_new.h"
#include "../share/net_msg_transfer.h"
#include "../share/net_event_manager.h"

class CProxyNetChannel :
	public ::common::net::CChannelBase,
	public ::common::CSingleton<CProxyNetChannel>
{
public:
	virtual void OnMsgEvent();

	virtual void OnConnect();

	virtual void OnDisconnect();
};
class CProxyService: 
	public IService,
	public ::common::CSingleton<CProxyService>
{
public:
	CProxyService();
	~CProxyService();

public:
	int32_t Initialize(void);
	int32_t Finalize();

	int32_t client_msg_dispatch(std::string& msg, unsigned long uid, ::common::CProxyHead& head);
	virtual EServiceType GetServiceType() { return PROXY_SERVICE; }

public:
	CProxyChannel* GetProxyChannel(uint32_t service_type, uint32_t service_id);
	bool AddProxyChannel(uint32_t service_type, uint32_t service_id, CProxyChannel* channel);
	//bool RemoveProxyChannel(uint32_t service_type, uint32_t service_id);

	//CWorldProxyClient& world_proxy_client() { return client_; }

	//void CheckPingTime();
public:
	virtual int32_t AddClientMsg(uint32_t socketId, uint32_t msgId, std::string &msg);
	virtual int32_t AddServerMsg(uint32_t msgId, std::string &msg, const common::CProxyHead& head);
	virtual void ProcessServerMsg(CProxySocketMsg& val);
	virtual void del_client_socket(uint32_t fd);
	virtual int32_t ServerStart();
private:
	typedef std::unordered_map<uint32_t, CProxyChannel*> TIDChannelMap;
	typedef std::unordered_map<uint32_t, TIDChannelMap> TChannelMap;

	//CServerNet server_net;
	TChannelMap channel_map_;
	common::net::CServerNet< CProxyNetChannel > proxy_net_;
};

#endif